import { takeEvery, put, all, takeLatest} from 'redux-saga/effects';
import axios from 'axios';

import {
    FETCH_HOME_MULTIDATA
} from './home/constants'

import {
    changeBannerAction,
    changeRecommendAction
} from './home/actionCreators'

function* fetchHomeMultidata(action) {
    const res = yield axios.get('http://123.207.32.32:8000/home/multidata');
    const banners = res.data.data.banner.list;
    const recommends = res.data.data.recommend.list;
    // yield put (changeBannerAction(banners));
    // yield put (changeRecommendAction(recommends));
    yield all ([
        yield put (changeBannerAction(banners)),
        yield put (changeRecommendAction(recommends))
    ])
}   

function* mySaga() {
    //takeLatest: 一次只能监听一个对应的action
    // takeEvery每一个都会被执行
    yield takeEvery(FETCH_HOME_MULTIDATA, fetchHomeMultidata)
    // yield all([
           //监听多个action
    //     takeLatest(FETCH_HOME_MULTIDATA, fetchHomeMultidata),
    //     takeLatest(FETCH_HOME_MULTIDATA, fetchHomeMultidata)

    // ])
}


export default mySaga;
// export default 生成器函数